﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using GastosDelHogar.WSGastos;

namespace GastosDelHogar.Controles
{
    public partial class ctrlCargarConcepto : UserControl
    {
        private ObservableCollection<DTConcepto> colConceptos { get; set; }
        WSGastosSoapClient ws = new WSGastosSoapClient();

        //Delegado para evento de error.
        public delegate void ErrorSummaryConceptoDelegate(string error);
        public event ErrorSummaryConceptoDelegate OnErrorSubscribed;

        //Delegado para evento cerrar ventana.
        public delegate void CerrarVentanaConceptoDelegate(Control control);
        public event CerrarVentanaConceptoDelegate OnCerrarVentaConcepto;

        public ctrlCargarConcepto()
        {
            InitializeComponent();
            CargarConceptos();
        }

        private void CargarConceptos()
        {
            this.Visibility = System.Windows.Visibility.Collapsed;
            GastosDelHogar.WSGastos.WSGastosSoapClient wsConceptos = new GastosDelHogar.WSGastos.WSGastosSoapClient();
            wsConceptos.GetConceptosCompleted += new EventHandler<GastosDelHogar.WSGastos.GetConceptosCompletedEventArgs>(wsConceptos_GetConceptosCompleted);
            wsConceptos.GetConceptosAsync();


        }

        void wsConceptos_GetConceptosCompleted(object sender, GastosDelHogar.WSGastos.GetConceptosCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                OnErrorSubscribed("Ocurrio un error al levantar los Conceptos");
                throw new Exception("Ocurrio un error al levantar los Conceptos" + Environment.NewLine + e.Error);
            }
            else
            {
                this.colConceptos = e.Result;
                cmbConceptos.ItemsSource = this.colConceptos;
                cmbConceptos.DisplayMemberPath = "Concepto";
                cmbConceptos.SelectedIndex = 0;
                this.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private void cmbConceptos_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DTConcepto conceptoSelected = (DTConcepto)cmbConceptos.SelectedItem;

            if (conceptoSelected != null)
            {
                LayoutRoot.DataContext = conceptoSelected;
            }
        }

        private void btnGuardar_Click(object sender, RoutedEventArgs e)
        {
            DTConcepto concepto = new DTConcepto() { Concepto = txtConceptoNombre.Text, Descripcion = txtConceptoDescription.Text, IdConcepto = -1 };
            ws.CrearConceptoAsync(concepto);
            ws.CrearConceptoCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(ws_CrearConceptoCompleted);
        }

        void ws_CrearConceptoCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                OnErrorSubscribed("Ocurrio un error al levantar los Conceptos");
            }
        }

        private void btnEliminar_Click(object sender, RoutedEventArgs e)
        {
            Dialog eliminar = new Dialog();
            eliminar.lblMsg.Text = "Esta por eliminar definitivamente el concepto: " + ((DTConcepto)cmbConceptos.SelectedItem).Concepto + Environment.NewLine + "Confirme operación.";
            eliminar.OnCerroDialogo += new Dialog.CerroDelegate(eliminar_OnCerroDialogo);
            eliminar.Show();
        }

        void eliminar_OnCerroDialogo(bool result)
        {
            if (result)
            {
                if (cmbConceptos.SelectedItem != null)
                {
                     ws.EliminarConceptoAsync((DTConcepto)cmbConceptos.SelectedItem);
                     ws.EliminarConceptoCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(ws_EliminarConceptoCompleted);
                }

            }
        }

        void ws_EliminarConceptoCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (cmbConceptos.SelectedItem !=null)
            {
                colConceptos.Remove((DTConcepto)cmbConceptos.SelectedItem);
            }
        }

        private void btnCancelar_Click(object sender, RoutedEventArgs e)
        {
            usrChartControl control = new usrChartControl();
            OnCerrarVentaConcepto(control);
        }

    }
}
